﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data;
using MyLib;
//using Portal.Entity;
//using Portal.Interface;
//using Portal.Logic;
using ProcessControl;

namespace POReceiptProcess
{
    public partial class NewPOReceiptProcess : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //提交前生成编号
            ((ButtonList)ButtonList1).BeforeSubmit += new System.ComponentModel.CancelEventHandler(NewRequest_BeforeSubmit);
        }

        /// <summary>
        /// 判断大小类是否有虚拟物品
        /// </summary>
        void NewRequest_BeforeSubmit(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //判断大小类是否有虚拟物品
            if (txtCATEGORY.Text.Trim().Length > 4)
            {
                if (txtCATEGORY.Text.Trim().Substring(0, 4) == "无形资产")
                {
                    fld_VIRTUAL.Text = "虚拟物品";
                }
                else
                {

                    fld_VIRTUAL.Text = "";
                }
            }

            if (txtSUBCATEGORIES.Text.Trim().Length > 4) {

                if (txtSUBCATEGORIES.Text.Trim().Substring(0, 4) == "无形资产") {
                    fld_VIRTUAL.Text = "虚拟物品";
                }
            }

        }

        //添加行
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            UserInfo userInfo = Page.FindControl("UserInfo1") as UserInfo;
            userInfo.AddNewRow(fld_detail_PROC_PORECEIPT_DT);
            BindDropDownList();
        }

        //删除行
        protected void fld_detail_PROC_PORECEIPT_DT_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            UserInfo userInfo = Page.FindControl("UserInfo1") as UserInfo;
            userInfo.DeleteRow(fld_detail_PROC_PORECEIPT_DT, e);
            BindDropDownList();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            BindDropDownList();
        }
        
        //绑定货物名称，物料类型
        void BindDropDownList()
        {

            //根据guid查询货物名称
            if (!string.IsNullOrEmpty(txtFormid.Text.Trim()))
            {
                string strQuery = "select MATERIALNAME,MATERIALTYPE from dbo.PROC_PO_DT where FORMID='" + txtFormid.Text.Trim()+"'";
                DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(strQuery);

                DropDownList ddlNAME = new DropDownList();
                DropDownList ddlTYPE = new DropDownList();
                TextBox txtName = new TextBox();
                TextBox txtTYPE = new TextBox();

                foreach (RepeaterItem re in fld_detail_PROC_PORECEIPT_DT.Items)
                {
                    ddlNAME = (DropDownList)re.FindControl("DDL_ITEMNAME");
                    ddlTYPE = (DropDownList)re.FindControl("DDL_ITEMTYPE");
                    txtName = (TextBox)re.FindControl("fld_ITEMNAME");
                    txtTYPE = (TextBox)re.FindControl("fld_ITEMTYPE");

                    //if (!string.IsNullOrEmpty(ddlNAME.SelectedValue))
                    //{
                    //    txtName.Text = ddlNAME.SelectedValue;
                    //}

                    //if (!string.IsNullOrEmpty(ddlTYPE.SelectedValue))
                    //{
                    //    txtTYPE.Text = ddlTYPE.SelectedValue;
                    //}

                    ddlNAME.Items.Clear();
                    ddlTYPE.Items.Clear();
                    ddlNAME.Items.Add(new ListItem("——请选择——", ""));
                    ddlTYPE.Items.Add(new ListItem("——请选择——", ""));
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {

                        if (!string.IsNullOrEmpty(dt.Rows[i]["MATERIALNAME"].ToString()))
                        {
                            ddlNAME.Items.Add(new ListItem(dt.Rows[i]["MATERIALNAME"].ToString(), dt.Rows[i]["MATERIALNAME"].ToString()));
                        }
                        if (!string.IsNullOrEmpty(dt.Rows[i]["MATERIALTYPE"].ToString()))
                        {
                            ddlTYPE.Items.Add(new ListItem(dt.Rows[i]["MATERIALTYPE"].ToString(), dt.Rows[i]["MATERIALTYPE"].ToString()));
                        }
                    }

                    if ( !string.IsNullOrEmpty(((TextBox)re.FindControl("fld_ITEMNAME")).Text))
                    {
                        ddlNAME.SelectedValue = ((TextBox)re.FindControl("fld_ITEMNAME")).Text;
                    }

                    if (!string.IsNullOrEmpty(((TextBox)re.FindControl("fld_ITEMTYPE")).Text))
                    {
                        ddlTYPE.SelectedValue = ((TextBox)re.FindControl("fld_ITEMTYPE")).Text;
                    }
                }
            }
        }

        //生成编号
        void CarteNo() {

            string temp = "";
            string idMark = "GR-" + DateTime.Now.ToString("yyyy");
            string strQuery = "select CONVERT(int,SUBSTRING(MAX(DOCUMENTNO),8,4)) from dbo.PROC_PORECEIPT";
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable(strQuery);
            if (!string.IsNullOrEmpty(dt.Rows[0][0].ToString()))
            {
                int no = Convert.ToInt32(dt.Rows[0][0].ToString());
                if (no < 10)
                {
                    temp = idMark + "000" + (no + 1);
                }
                if (no < 100 && no >= 10)
                {

                    temp = idMark + "00" + (no + 1);
                }
                if (no < 1000 && no >= 100)
                {
                    temp = idMark + "0" + (no + 1);
                }
                if (no >= 1000)
                {
                    temp = idMark + (no + 1);
                }
            }
            else
            {
                temp = idMark + "0001";
            }
            //(((UserInfo)UserInfo1).FindControl("fld_DOCUMENTNO") as TextBox).Text = temp;
        }

        protected void ITEMNAME_OnSelectedIndexChanged(object sender, EventArgs e) {

            DropDownList ddlNAME = (DropDownList)sender;
            TextBox txtName = (TextBox)ddlNAME.Parent.FindControl("fld_ITEMNAME");
            txtName.Text = ddlNAME.SelectedValue;
        }

        protected void ITEMTYPE_OnSelectedIndexChanged(object sender, EventArgs e) {

            DropDownList ddlTYPE = (DropDownList)sender;
            TextBox txtTYPE = (TextBox)ddlTYPE.Parent.FindControl("fld_ITEMTYPE");
            txtTYPE.Text = ddlTYPE.SelectedValue;
        }
    }
}